<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Wap\Controller;

use Think\Controller;
use Com\DingdingAuth;
/**
 * 后台用户控制器
 * 
 * @author jry <598821125@qq.com>
 */
class ProjectController extends WapController
{
	
    // 项目信息列表
    public function all()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	$ding_id=$user_info['userid'];
    	$ding_pass=M('Employee')->where(array('ding_id'=>$ding_id))->getField('ding_pass');
    	if (!$ding_pass) {
    		$this->error('没有权限！');
    	}
        // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array(
            'like',
            '%' . $keyword . '%'
        );
        $map['project_number|project_name|status'] = array(
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );
        
        $keywords = I('keywords');
        if (! empty($keywords)) {
            $map['status'] = $keywords;
        }
        
        
        // 获取所有用户
        
        $data_list = D('Project')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, 10)
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new \Common\Util\Page(D('Project')->where($map)->count(), 10);
        
        foreach ($data_list as $key => &$val) {
            $vall['customerid'] = $val['customerid'];
            $val['customer_service_id'] = get_employee_info($val['customer_service_id'], 'name');
            $val['kh_id']=$vall['customerid'];
            $val['customerid'] = get_customer_info($val['customerid'], 'customer_name');
            
            // $item['title'] = '<a href="'.U('Document/index', array('cid' => $item['id'])).'">'.$item['title'].'</a>';
//             $val['customerid'] = '<a href="' . U('Customer/xq', array(
//                 'id' => $vall['customerid']
//             )) . '" style="text-decoration:none;">' . $val['customerid'] . '</a>';
            if($val['responsible_person']){
                $developer =json_decode($val['developer']) ;
                $developer_txt='';
                foreach ($developer as $val2){
                    if(empty($developer_txt)){
                        $developer_txt= get_employee_info($val2, 'name');
                    }
                    else{
                        $developer_txt.= ','.get_employee_info($val2, 'name');
                    }
                }
                $val['responsible_person'] =get_employee_info($val['responsible_person'], 'name');
                //$val['responsible_person'] = '<a href="#" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="'.$developer_txt.'">'.get_employee_info($val['responsible_person'], 'name').'</a>';
            }
            $val['contract_start_end_time'] = date('Y/m/d', $val['contract_start_time']) . '~' . date('Y/m/d', $val['contract_end_time']);
            $val['status_text']=get_descriptions_title('project_status', 'project_status', $val['status']);
           
        }
        
   

        $project_status = D('Descriptions')->where("tablename='project_status'")->select();
 
        
        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('project_status', $project_status);
       
        $this->assign('meta_title', '全部项目');
        $this->display();
        
    }

    public function index()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	$ding_id=$user_info['userid'];
    	$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
        // 搜索
        $keyword = I('keyword', '', 'string');
        $condition = array(
            'like',
            '%' . $keyword . '%'
        );
        $map['project_number|project_name|status'] = array(
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );
        
        $keywords = I('keywords');
        if (! empty($keywords)) {
            $map['status'] = $keywords;
        }
              
        
       /*判断是否为项目负责人*/
       $resa=M("UserGroup")->where(array("responsible_person"=>$uid))->where("id>1")->getField("id",true);
      /* echo M()->getlastSql();
       print_r($resa);exit;*/
       
        if($resa){
        	$uid=M("Employee")->where(array("department"=>array("in",$resa)))->getField("uid",true);
        	//$map['developer'] = array("in",$uid);
        	$map['responsible_person'] = array("in",$uid);
        }else{
        	// $map['developer|customer_service_id'] = array($uid,$uid,'_multi' => true);
        	$map['responsible_person|customer_service_id'] = array($uid,$uid,'_multi' => true);
        }
       
        // 获取所有用户
        
        $data_list = D('Project')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, 10)->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Project')->where($map)->count(), 10);
        
     foreach ($data_list as $key => &$val) {
            $vall['customerid'] = $val['customerid'];
            $val['customer_service_id'] = get_employee_info($val['customer_service_id'], 'name');
            $val['customerid'] = get_customer_info($val['customerid'], 'customer_name');
            
            // $item['title'] = '<a href="'.U('Document/index', array('cid' => $item['id'])).'">'.$item['title'].'</a>';
//             $val['customerid'] = '<a href="' . U('Customer/xq', array(
//                 'id' => $vall['customerid']
//             )) . '" style="text-decoration:none;">' . $val['customerid'] . '</a>';
            if($val['responsible_person']){
                $developer =json_decode($val['developer']) ;
                $developer_txt='';
                foreach ($developer as $val2){
                    if(empty($developer_txt)){
                        $developer_txt= get_employee_info($val2, 'name');
                    }
                    else{
                        $developer_txt.= ','.get_employee_info($val2, 'name');
                    }
                }
                $val['responsible_person'] =get_employee_info($val['responsible_person'], 'name');
                //$val['responsible_person'] = '<a href="#" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="'.$developer_txt.'">'.get_employee_info($val['responsible_person'], 'name').'</a>';
            }
            $val['contract_start_end_time'] = date('Y/m/d', $val['contract_start_time']) . '~' . date('Y/m/d', $val['contract_end_time']);
            $val['status_text']=get_descriptions_title('project_status', 'project_status', $val['status']);
           
        }
        
  
        $project_status = D('Descriptions')->where("tablename='project_status'")->select();
        
 
        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('project_status', $project_status);
        
        $this->assign('meta_title', '我的项目');
        $this->display();
    }
    
    /**
     * 新增项目
     */
    public function add()
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
        //$customerid = I('customerid');
        // if(empty($customerid)){
        // $customerid='00';
        // }
        if (IS_POST) {
        	//dump($_POST);exit;
        	for ( $i = 1; $i <= 5; $i++)
        	{
	        	if ($_POST["uid".$i.""]) {
	        		
	        		$uid=M('Employee')->where(array('ding_id'=>$_POST["uid".$i.""]))->getField('uid');
	        		$uid_str .=",".$uid;
	        	}
        				 
        	}
        	$uid_str=substr($uid_str,1);
        	$developer=explode(',',$uid_str);
           // $developer = I("developer");
            $_POST['developer']=json_encode($developer);
            $_POST['responsible_person'] = M('Employee')->where(array('ding_id'=>$_POST["responsible_person"]))->getField('uid');
            $_POST['contract_start_time'] = strtotime($_POST['contract_start_time']);
            $_POST['contract_end_time'] = strtotime($_POST['contract_end_time']);
            $project_object = D('Project');
            $data = $project_object->create();
            if ($data) {
                $id = $project_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($project_object->getError());
            }
        } else {
            $kefu_arr = M("employee")->where(array("department"=>7,"status"=>1))->field("uid as id,name as title")->select();
            
            $cus=M("Customer")->field("id,customer_name")->select();
            foreach($cus as $k=>$v){
            	$cuss[$v['id']]=$v['customer_name'];
            }
            
            $this->assign('customer', $cuss);
            $this->assign('kefu_arr', $kefu_arr);
            $this->assign('customer_level', $customer_level);
            
            $this->assign('meta_title', '新增项目');
            $this->display();
            
        }
    }

    

    /**
     * 编辑
     * 
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
        if (IS_POST) {
        	for ( $i = 1; $i <= 5; $i++)
        	{
        	if ($_POST["uid".$i.""]) {
        	 
        			$uid=M('Employee')->where(array('ding_id'=>$_POST["uid".$i.""]))->getField('uid');
        			$uid_str .=",".$uid;
        	}
        	 
        	}
        			$uid_str=substr($uid_str,1);
        			$developer=explode(',',$uid_str);
        			
        			$_POST['developer']=json_encode($developer);
        			$_POST['responsible_person'] = M('Employee')->where(array('ding_id'=>$_POST["responsible_person"]))->getField('uid');
        			$_POST['contract_start_time'] = strtotime($_POST['contract_start_time']);
        			$_POST['contract_end_time'] = strtotime($_POST['contract_end_time']);
        			
            $project_object = D('Project');
            
            if ($project_object->save($_POST)) {
                $this->success('更新成功', U('index'));
            } else {
                $this->error('更新失败', $project_object->getError());
            }
        } else {
            
            $info = D('Project')->find($id);
            $kefu_arr = M("employee")->where(array("department"=>7,"status"=>1))->field("uid as id,name as title")->select();
            
            $cus=M("Customer")->field("id,customer_name")->select();
            foreach($cus as $k=>$v){
            	$cuss[$v['id']]=$v['customer_name'];
            }
            
           $responsible_person['id']=M('Employee')->where(array('uid'=>$info['responsible_person']))->getField('ding_id');
           $responsible_person['name']=M('Employee')->where(array('uid'=>$info['responsible_person']))->getField('name');
           $kaifa_arr=json_decode($info['developer'],true);
           foreach ($kaifa_arr as $k=> $v){
           	$kaifa[$k]=M('Employee')->where(array('uid'=>$v))->find();
           }
           //dump($responsible_person); dump($kaifa);exit;
           $this->assign('header', $responsible_person);
           $this->assign('kaifa', $kaifa);
            $this->assign('info', $info);
            $this->assign('customer', $cuss);
            $this->assign('kefu_arr', $kefu_arr);
            $this->assign('customer_level', $customer_level);
            
            $this->assign('meta_title', '编辑项目');
            $this->display();
        }
    }

    public function status($id)
    {
    	
        $map['id'] = $id;
        $status = D('Project')->where($map)->getField('status');
        if ($status == 1) {
            $res = D('Project')->where($map)->setField('status', '2');
            D('Project')->where($map)->setField('real_start_time', time());
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 2) {
            $res = D('Project')->where($map)->setField('status', '3');
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 3) {
            $res = D('Project')->where($map)->setField('status', '4');
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 4) {
            $res = D('Project')->where($map)->setField('status', '5');
            D('Project')->where($map)->setField('real_end_time', time());
            if ($res) {
                redirect(U('Project/index'));
            }
        }
        if ($status == 5) {
        	
        		$this->success('该项目已完成', U('index'));
        
        }
    }
    
    /**
     * 项目详情页
     */
    public function projectinfo(){
    	$user_info=session('DING_USER');
    	if (!$user_info) {
    		header('Location: '.U('Wap/Index/index'));
    	}
    	$id=I('id');
    	$map['id']=$id;
    	$val = M('Project')->where($map)->find();
    	$vall['customerid'] = $val['customerid'];
    	$val['customer_service_id'] = get_employee_info($val['customer_service_id'], 'name');
    	$val['customerid'] = get_customer_info($val['customerid'], 'customer_name');
    	
    	// $item['title'] = '<a href="'.U('Document/index', array('cid' => $item['id'])).'">'.$item['title'].'</a>';
//     	$val['customerid'] = '<a href="' . U('Customer/xq', array(
//     			'id' => $vall['customerid']
//     	)) . '" style="text-decoration:none;">' . $val['customerid'] . '</a>';
    	if($val['responsible_person']){
    		$developer =json_decode($val['developer']) ;
    		$developer_txt='';
    		foreach ($developer as $val2){
    			if(empty($developer_txt)){
    				$developer_txt= get_employee_info($val2, 'name');
    			}
    			else{
    				$developer_txt.= ','.get_employee_info($val2, 'name');
    			}
    		}
    		//$val['responsible_person'] = '<a href="#" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="'.$developer_txt.'">'.get_employee_info($val['responsible_person'], 'name').'</a>';
    		$val['responsible_person'] =get_employee_info($val['responsible_person'], 'name');
    	}
    	$val['contract_start_end_time'] = date('Y/m/d', $val['contract_start_time']) . '~' . date('Y/m/d', $val['contract_end_time']);
    	$val['status_text']=get_descriptions_title('project_status', 'project_status', $val['status']);
    	 
    	$this->assign('val', $val);
    	$this->assign('meta_title', '项目详情');
    	$this->display();
    	
    }

    /**
     * 删除项目
     */
//     public function delProjectAjax(){
    
//     	$id = I('id');
//     	$re=M('Project')->where('id='.$id)->delete();
//     	if ($re) {
//     		$data['info']=1;
//     	}else{
//     		$data['info']=0;
//     	}
//     	$this->ajaxReturn($data);
//     }
    
    
}